package top.lunarye.api.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import top.lunarye.api.action.AfterParamCheckAction;
import top.lunarye.api.action.AssembleAction;
import top.lunarye.api.action.PreParamCheckAction;
import top.lunarye.api.action.SendMQAction;
import top.lunarye.api.enums.BusinessCode;
import top.lunarye.support.pipeline.BusinessProcess;
import top.lunarye.support.pipeline.ProcessController;
import top.lunarye.support.pipeline.ProcessTemplate;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;

/**
 * 发送管道的配置类
 * @author lunar
 * @since 2022-07-14
 */
@Configuration
public class PipelineConfig {

    @Resource
    private PreParamCheckAction preParamCheckAction;

    @Resource
    private AssembleAction assembleAction;

    @Resource
    private AfterParamCheckAction afterParamCheckAction;

    @Resource
    private SendMQAction sendMQAction;

    /**
     * 普通发送执行流程
     * 1. 前置参数检测
     * 2. 组装参数
     * 3. 后置参数检测
     * 4. 发送消息到MQ
     * @return
     */
    @Bean("commonSendTemplate")
    public ProcessTemplate commonSendTemplate() {
        ProcessTemplate processTemplate = new ProcessTemplate();
        processTemplate.setProcessList(Arrays.asList(preParamCheckAction, assembleAction,
                afterParamCheckAction, sendMQAction));
        return processTemplate;
    }

    @Bean
    public ProcessController processController() {
        ProcessController processController = new ProcessController();
        HashMap<String, ProcessTemplate> templateConfig = new HashMap<>(4);
        templateConfig.put(BusinessCode.COMMON_SEND.getCode(), commonSendTemplate());
        processController.setTemplateConfig(templateConfig);
        return processController;
    }

}
